<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/10
 * Time: 13:46
 */

namespace Timer\Models;


use Timer\Lib\G_CONST;
use Timer\Lib\OPT_CONST;
use Timer\Module\Logger;
use Timer\Module\Socket;
use Timer\Module\Tools;
use Workerman\Lib\Timer;

class TimerModel
{

    /**
     * 添加timer
     * @param $arrData
     * @return array|int
     */
    public function buildTimer($arrData){
        $operation = $arrData['operation'];
        $data = $arrData['data'];
        $room_id = $arrData['room_id'];

        if(!isset($data['limit_time']) || trim($data['limit_time']) === G_CONST::EMPTY_STRING || $data['limit_time'] <= 0 || !is_numeric($data['limit_time']))
        {
            Logger::getInstance()->error("function(buildTimer):lack of limit_time"." in file".__FILE__." on Line ".__LINE__);
            return $this->_missingPrameterArr($operation,"limit_time");
        }
        if(! (isset($data['callback_array']['operation']) && isset($data['callback_array']['room_id']) && isset($data['callback_array']['data']) ) )
        {
            Logger::getInstance()->error( "function(buildTimer):lack of callback_array or invalid"." in file".__FILE__." on Line ".__LINE__);
            return $this->_missingPrameterArr($operation,"callback_array");
        }
        $callback_array = $data['callback_array'];
        $timer_id = 0;
        $timer_id = Timer::add($data['limit_time'], array($this, 'timeTrigger'), array(&$timer_id,$callback_array), false);

        //推送给管理端
        $send_array = array(
            'operation' => "ReturnTimerid",
            'room_id' => $room_id,
            'data' => array(
                'timer_id' => $timer_id
            )
        );
        @$this->sendServerMgnt($send_array);

        return OPT_CONST::NO_RETURN;
    }

    /**
     * 推给游戏端
     * @param string $msg_array
     * @return bool
     */
    protected function sendServerMgnt($msg_array = ""){
        var_dump($msg_array);
        $message = Tools::getInstance()->jsonEncode($msg_array);
        $socket_name = "GameServer";
        Socket::getInstance()->sendMessageToSocket($socket_name,$message);
        return true;
    }

    public function timeTrigger($timer_id,$callback_array){
        //推送给管理端
        $callback_array['data']['timer_id'] = $timer_id;
        @$this->sendServerMgnt($callback_array);
        return true;
    }

    /**
     * 释放timer
     * @param $arrData
     * @return array|int
     */
    public function deleteTimer($arrData){
        $operation = $arrData['operation'];
        $data = $arrData['data'];
        if(!isset($data['timer_id']) || trim($data['timer_id']) === G_CONST::EMPTY_STRING || $data['timer_id'] <= 0) {
            Logger::getInstance()->error("function(releaseTimer):lack of timer_id"." in file".__FILE__." on Line ".__LINE__);
            return $this->_missingPrameterArr($operation,"timer_id");
        }
        Timer::del((int)$data['timer_id']);
        return OPT_CONST::NO_RETURN;
    }

    /**
     * 返回缺参结果
     * @param $operation
     * @param $prameter
     * @return array
     */
    protected function _missingPrameterArr($operation,$prameter){
        return array('result'=>OPT_CONST::MISSING_PARAMETER,'operation'=>$operation,'data'=>array("missing_parameter"=>$prameter),'result_message'=>"缺少参数");
    }
}